In [1]:
import requests

In [2]:
req=requests.get('http://httpbin.org/get')

In [3]:
print(req.text)


{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.12.4"
  }, 
  "origin": "130.56.224.144", 
  "url": "http://httpbin.org/get"
}


In [4]:
parameters={'user':'xianjunzheng','password':'1234'}
req=requests.post('http://httpbin.org/post',params=parameters)

In [5]:
print(req.text)


{
  "args": {
    "password": "1234", 
    "user": "xianjunzheng"
  }, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "0", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.12.4"
  }, 
  "json": null, 
  "origin": "130.56.224.144", 
  "url": "http://httpbin.org/post?user=xianjunzheng&password=1234"
}

基本的登陆


In [6]:
url='http://httpbin.org'

In [7]:
req=requests.get(url+'/basic-auth/user/passwd',auth=('user','passwd'))

In [8]:
print(req.text)


{
  "authenticated": true, 
  "user": "user"
}


In [9]:
print(req.url)


http://httpbin.org/basic-auth/user/passwd

In [10]:
print(req.status_code)


200

In [11]:
import json

In [12]:
payload={'some':'data'}
headers={'Content-Type':'application/json','Authorization':'some token'}
req=requests.post(url+'/post',data=json.dumps(payload),headers=headers)

In [13]:
print(req.text)


{
  "args": {}, 
  "data": "{\"some\": \"data\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Authorization": "some token", 
    "Connection": "close", 
    "Content-Length": "16", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.12.4"
  }, 
  "json": {
    "some": "data"
  }, 
  "origin": "130.56.224.144", 
  "url": "http://httpbin.org/post"
}


In [14]:
req=requests.post(url+'/post',data=payload)

In [15]:
print(req.text)


{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "some": "data"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "9", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.12.4"
  }, 
  "json": null, 
  "origin": "130.56.224.144", 
  "url": "http://httpbin.org/post"
}


In [16]:
files={'file':open('dump.txt','rb')}

In [17]:
req=requests.post(url+'/post',files=files)

In [18]:
print(req.text)


{
  "args": {}, 
  "data": "", 
  "files": {
    "file": "dump\ndump\nfile\nfile"
  }, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "163", 
    "Content-Type": "multipart/form-data; boundary=27242f1fd7024cf3b4e515d04ddca27e", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.12.4"
  }, 
  "json": null, 
  "origin": "130.56.224.144", 
  "url": "http://httpbin.org/post"
}


In [19]:
print(req.status_code)


200

In [20]:
req.status_code==requests.codes.ok


Out[20]:
True
This is server's response headers and it is cse insensitive

In [21]:
print(req.headers)


{'Content-Type': 'application/json', 'Server': 'gunicorn/19.7.1', 'Content-Length': '488', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Connection': 'keep-alive', 'Date': 'Fri, 24 Mar 2017 04:03:59 GMT', 'Via': '1.1 vegur'}

In [22]:
req.headers['content-type']


Out[22]:
'application/json'

sending cookie


In [23]:
cookies={}
cookies['cookie']='cookie-value'

In [24]:
req=requests.get(url+'/cookies',cookies=cookies)

In [25]:
print(req.text)


{
  "cookies": {
    "cookie": "cookie-value"
  }
}

Re-direction 302 means the URL has been redirected to some other location. We could use allow_redirects=False to disable this feature.


In [26]:
req=requests.head('http://www.google.com',allow_redirects=True)

In [27]:
print(req.url)


http://www.google.com.au/?gfe_rd=cr&ei=sZrUWM6EFqbM8gemxJeADg

In [28]:
req.history


Out[28]:
[<Response [302]>]

In [29]:
req=requests.head('http://www.google.com',allow_redirects=False)

In [30]:
print(req.text)



time out


In [31]:
try:
    req=requests.get('http://google.com',timeout=0.03)
except BaseException as e:
    print("It is exceeding timeout")
    print(str(e))


It is exceeding timeout
HTTPConnectionPool(host='www.google.com.au', port=80): Read timed out. (read timeout=0.03)

session session can persist cookies across requests


In [32]:
import requests
session=requests.Session()

In [33]:
session.headers.update(headers)
session.data='some data here'
session.params={'key1':'value1','key2':'value2'}
session.auth=('user','passwd')

In [34]:
req=session.get(url+'/basic-auth/user/passwd')

In [35]:
print(req.text)


{
  "authenticated": true, 
  "user": "user"
}


In [36]:
req=session.get(url+'/get')

In [37]:
print(req.text)


{
  "args": {
    "key1": "value1", 
    "key2": "value2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Authorization": "Basic dXNlcjpwYXNzd2Q=", 
    "Connection": "close", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.12.4"
  }, 
  "origin": "130.56.224.144", 
  "url": "http://httpbin.org/get?key1=value1&key2=value2"
}


In [38]:
req=session.post(url+'/post',data='some data here')

In [39]:
print(req.text)


{
  "args": {
    "key1": "value1", 
    "key2": "value2"
  }, 
  "data": "some data here", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Authorization": "Basic dXNlcjpwYXNzd2Q=", 
    "Connection": "close", 
    "Content-Length": "14", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.12.4"
  }, 
  "json": null, 
  "origin": "130.56.224.144", 
  "url": "http://httpbin.org/post?key1=value1&key2=value2"
}

session can be overriden


In [40]:
req=session.get(url+'/get',headers={'Content-Type':'application','User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'})

In [41]:
print(req.text)


{
  "args": {
    "key1": "value1", 
    "key2": "value2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Authorization": "Basic dXNlcjpwYXNzd2Q=", 
    "Connection": "close", 
    "Content-Type": "application", 
    "Host": "httpbin.org", 
    "User-Agent": "linux safari"
  }, 
  "origin": "130.56.224.144", 
  "url": "http://httpbin.org/get?key1=value1&key2=value2"
}

这里的req是server返回的response 当requests.get()或者session.get(),首先发生的事情是构造了一个request,这个request将被发送个server,同时req里也将保存这个request 接下来req就接收server返回的response


In [47]:
print(req.request)
print(req.request.headers)
print(req.request.body)


<PreparedRequest [GET]>
{'Content-Type': 'application', 'Accept': '*/*', 'Authorization': 'Basic dXNlcjpwYXNzd2Q=', 'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'linux safari'}
None

In [49]:
print(req.headers)
print(req.text)


{'Content-Type': 'application/json', 'Server': 'gunicorn/19.7.1', 'Content-Length': '412', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Connection': 'keep-alive', 'Date': 'Fri, 24 Mar 2017 04:04:04 GMT', 'Via': '1.1 vegur'}
{
  "args": {
    "key1": "value1", 
    "key2": "value2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Authorization": "Basic dXNlcjpwYXNzd2Q=", 
    "Connection": "close", 
    "Content-Type": "application", 
    "Host": "httpbin.org", 
    "User-Agent": "linux safari"
  }, 
  "origin": "130.56.224.144", 
  "url": "http://httpbin.org/get?key1=value1&key2=value2"
}


In [ ]: